/**
 * Created by Elsa on 2017/8/29.
 */
layui.define(['form','jquery'], function(exports){
    var form = layui.form(),
        $ = layui.jquery,
        layer = layui.layer;

    var rgstnum = getQueryString("rgstnum");

    $.ajax({
        type:"GET",
        url:contextPath+"/project/ops/ct/"+ rgstnum ,
        contentType: "application/json; charset=utf-8",
        dataType:"json",
        success:function (data) {

            console.log(data);
            $('input[name="shipname"]').attr("value",data.shipname);
            $('input[name="rgstnum"]').attr("value",data.rgstnum);

            if(data.ctdate){
                $('input[name="ctname"]').attr("value",data.ctname);
                $('input[name="ctdate"]').attr("value",data.ctdate);

                $('#span1').html(data.ctt1jg);
                $('input[name="ctt1jg"][value="'+data.ctt1jg+'"]').attr("checked",true);
                $('#span1x').html(data.ctt1bz);
                $('input[name="re1"]').attr("value",data.ctt1bz);

                $('#span2').html(data.ctt2jg);
                $('input[name="ctt2jg"][value="'+data.ctt2jg+'"]').attr("checked",true);
                $('#span2x').html(data.ctt2bz);
                $('input[name="re2"]').attr("value",data.ctt2bz);

                $('#span3').html(data.ctt3jg);
                $('input[name="ctt3jg"][value="'+data.ctt3jg+'"]').attr("checked",true);
                $('#span3x').html(data.ctt3bz);
                $('input[name="re3"]').attr("value",data.ctt3bz);

                $('#span4').html(data.ctt4jg);
                $('input[name="ctt4jg"][value="'+data.ctt4jg+'"]').attr("checked",true);
                $('#span4x').html(data.ctt4bz);
                $('input[name="re4"]').attr("value",data.ctt4bz);

                $('#span5').html(data.ctt5jg);
                $('input[name="ctt5jg"][value="'+data.ctt5jg+'"]').attr("checked",true);
                $('#span5x').html(data.ctt5bz);
                $('input[name="re5"]').attr("value",data.ctt5bz);

                $('#span6').html(data.ctt6jg);
                $('input[name="ctt6jg"][value="'+data.ctt6jg+'"]').attr("checked",true);
                $('#span6x').html(data.ctt6bz);
                $('input[name="re6"]').attr("value",data.ctt6bz);

                $('#span7').html(data.ctt7jg);
                $('input[name="ctt7jg"][value="'+data.ctt7jg+'"]').attr("checked",true);
                $('#span7x').html(data.ctt7bz);
                $('input[name="re7"]').attr("value",data.ctt7bz);

                $('#span8').html(data.ctt8jg);
                $('input[name="ctt8jg"][value="'+data.ctt8jg+'"]').attr("checked",true);
                $('#span8x').html(data.ctt8bz);
                $('input[name="re8"]').attr("value",data.ctt8bz);

                $('#span9').html(data.ctt9jg);
                $('input[name="ctt9jg"][value="'+data.ctt9jg+'"]').attr("checked",true);
                $('#span9x').html(data.ctt9bz);
                $('input[name="re9"]').attr("value",data.ctt9bz);

                $('#span10').html(data.ctt10jg);
                $('input[name="ctt10jg"][value="'+data.ctt10jg+'"]').attr("checked",true);
                $('#span10x').html(data.ctt10bz);
                $('input[name="re10"]').attr("value",data.ctt10bz);

                $('#span11').html(data.ctt11jg);
                $('input[name="ctt11jg"][value="'+data.ctt11jg+'"]').attr("checked",true);
                $('#span11x').html(data.ctt11bz);
                $('input[name="re11"]').attr("value",data.ctt11bz);

                $('#sus').show();
                $('#su').on('click',function () {
                    window.open('../../../print/opera/ExperRecord/ct/OperaCtRecord.html?rgstnum='+rgstnum);
                    return false;
                });

                $.ajax({
                    type:"GET",
                    url:contextPath+"/personal/role",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        if(data.role=="管理员"){
                            $('#edit').show();
                        }else if(data.role=="验船师"){
                            $.ajax({
                                type: "GET",
                                url: contextPath + "/project/ops/" + rgstnum,
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                success: function (data) {
                                    console.log(data.allstate);
                                    if(data.allstate!="完成"){
                                       $('#edit').show(); 
                                    }
                                },
                                error: function (e) {
                                }
                            });
                        }
                    },
                     error: function (e) {

                    }
                });


                $('#edit').on('click',function () {
                    $('#sus').hide();
                    $('#sss').show();

                    $('span.ctstate1').hide();
                    $('div.ctstate2').show();
                    $('input.ctstate2').show();

                    $('input[name="ctname"]').attr('disabled', false);
                    $('input[name="ctdate"]').attr('disabled', false);

                    $('input[name="ctname"]').attr('class', 'layui-input');
                    $('input[name="ctdate"]').attr('class', 'layui-input');
            
                    return false;

                });

                $('#submit').on('click',function () {
                    $(this).attr('disabled','disabled');
                    var ss = new Object();

                    ss.rgstnum = $('input[name="rgstnum"]').val();
                    ss.ctname = $('input[name="ctname"]').val();

                    if($('input[name="ctt1jg"]:checked').val()){
                        ss.ctt1jg = $('input[name="ctt1jg"]:checked').val();
                    }else{
                        ss.ctt1jg = ""; 
                    }
                    if($('input[name="ctt2jg"]:checked').val()){
                        ss.ctt2jg = $('input[name="ctt2jg"]:checked').val();
                    }else{
                        ss.ctt2jg = ""; 
                    }
                    if($('input[name="ctt3jg"]:checked').val()){
                        ss.ctt3jg = $('input[name="ctt3jg"]:checked').val();
                    }else{
                        ss.ctt3jg = ""; 
                    }
                    if($('input[name="ctt4jg"]:checked').val()){
                        ss.ctt4jg = $('input[name="ctt4jg"]:checked').val();
                    }else{
                        ss.ctt4jg = ""; 
                    }
                    if($('input[name="ctt5jg"]:checked').val()){
                        ss.ctt5jg = $('input[name="ctt5jg"]:checked').val();
                    }else{
                        ss.ctt5jg = ""; 
                    }
                    if($('input[name="ctt6jg"]:checked').val()){
                        ss.ctt6jg = $('input[name="ctt6jg"]:checked').val();
                    }else{
                        ss.ctt6jg = ""; 
                    }
                    if($('input[name="ctt7jg"]:checked').val()){
                        ss.ctt7jg = $('input[name="ctt7jg"]:checked').val();
                    }else{
                        ss.ctt7jg = ""; 
                    }
                    if($('input[name="ctt8jg"]:checked').val()){
                        ss.ctt8jg = $('input[name="ctt8jg"]:checked').val();
                    }else{
                        ss.ctt8jg = ""; 
                    }
                    if($('input[name="ctt9jg"]:checked').val()){
                        ss.ctt9jg = $('input[name="ctt9jg"]:checked').val();
                    }else{
                        ss.ctt9jg = ""; 
                    }
                    if($('input[name="ctt10jg"]:checked').val()){
                        ss.ctt10jg = $('input[name="ctt10jg"]:checked').val();
                    }else{
                        ss.ctt10jg = ""; 
                    }
                    if($('input[name="ctt11jg"]:checked').val()){
                        ss.ctt11jg = $('input[name="ctt11jg"]:checked').val();
                    }else{
                        ss.ctt11jg = ""; 
                    }
                    
                    ss.ctt1bz = $('input[name="re1"]').val();
                    ss.ctt2bz = $('input[name="re2"]').val();
                    ss.ctt3bz = $('input[name="re3"]').val();
                    ss.ctt4bz = $('input[name="re4"]').val();
                    ss.ctt5bz = $('input[name="re5"]').val();
                    ss.ctt6bz = $('input[name="re6"]').val();
                    ss.ctt7bz = $('input[name="re7"]').val();
                    ss.ctt8bz = $('input[name="re8"]').val();
                    ss.ctt9bz = $('input[name="re9"]').val();
                    ss.ctt10bz = $('input[name="re10"]').val();
                    ss.ctt11bz = $('input[name="re11"]').val();

                    console.log(JSON.stringify(ss));

                    $.ajax({
                        type:"PUT",
                        url:contextPath+"/project/ops/ct/update",
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify(ss),
                        dataType:"json",
                        success:function (data) {
                            console.log(data.message);
                            if(data.message == "更新船体设备效益试验记录表成功!"){
                                layer.msg('船体设备效益试验记录表更新成功!<br>页面将在1秒后刷新...');
                                setTimeout(function(){window.location.reload();},1000);
                            } else {
                                $('#submit').attr('disabled',false);
                                layer.msg(data.message, {
                                    time:false,
                                    btn:"确定",
                                    btnAlign: 'c'
                                });
                                $("#all-info").html("*"+data.message);
                            }
                        },
                        error:function (e) {
                        }
                    });
                    return false;
                });

                $('#refresh').on('click',function () {
                    $('#sus').show();
                    $('#sss').hide();

                    $('span.ctstate1').show();
                    $('div.ctstate2').hide();
                    $('input.ctstate2').hide();

                    $('input[name="ctname"]').attr('disabled', true);
                    $('input[name="ctdate"]').attr('disabled', true);

                    $('input[name="ctname"]').attr('class', 'layui-input layui-disabled');
                    $('input[name="ctdate"]').attr('class', 'layui-input layui-disabled');
            
                    return false;
                });
                
            } else {
                $.ajax({
                    type:"GET",
                    url:contextPath+"/personal/role",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        console.log(data.role);
                        if(data.role=="管理员"||data.role=="验船师"){
                            $('span.ctstate1').hide();
                            $('div.ctstate2').show();
                            $('input.ctstate2').show();
                            $('div#submit').show();
                            $.ajax({
                                type: "GET",
                                url: contextPath + "/personal/info",
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                success: function (data) {
                                    $('input[name="ctname"]').attr("value",data.name);
                                },
                                error: function (e) {

                                }
                            });
                            $('input[name="ctdate"]').attr("value",getNowFormatDate());
                        }else if(data.role=="船东"||data.role=="船厂"){
                            $('span.ctstate1').hide();
                        }else{

                        }
                    },
                    error: function (e) {
                    }
                });
            }
        },
        error:function (e) {

        }
    });

    //自定义验证规则
    form.verify({
        title: function(value) {
            if(value.length < 5) {
                return '标题至少得5个字符啊';
            }
        },
        pass: [/(.+){6,12}$/, '密码必须6到12位'],
        content: function(value) {
            layedit.sync(editIndex);
        }
    });

    //监听提交
    form.on('submit(caseglyx)', function(data) {
        $(this).attr('disabled','disabled');
        var OperaCtRecord = new Object();
        OperaCtRecord.rgstnum = data.field.rgstnum;
        OperaCtRecord.ctname = data.field.ctname;
        if($('input[name="ctt1jg"]:checked').val()){
            OperaCtRecord.ctt1jg = $('input[name="ctt1jg"]:checked').val();
        }else{
            OperaCtRecord.ctt1jg = "";
        }
        if($('input[name="ctt2jg"]:checked').val()){
            OperaCtRecord.ctt2jg = $('input[name="ctt2jg"]:checked').val();
        }else{
            OperaCtRecord.ctt2jg = "";
        }
        if($('input[name="ctt3jg"]:checked').val()){
            OperaCtRecord.ctt3jg = $('input[name="ctt3jg"]:checked').val();
        }else{
            OperaCtRecord.ctt3jg = "";
        }
        if($('input[name="ctt4jg"]:checked').val()){
            OperaCtRecord.ctt4jg = $('input[name="ctt4jg"]:checked').val();
        }else{
            OperaCtRecord.ctt4jg = "";
        }
        if($('input[name="ctt5jg"]:checked').val()){
            OperaCtRecord.ctt5jg = $('input[name="ctt5jg"]:checked').val();
        }else{
            OperaCtRecord.ctt5jg = "";
        }
        if($('input[name="ctt6jg"]:checked').val()){
            OperaCtRecord.ctt6jg = $('input[name="ctt6jg"]:checked').val();
        }else{
            OperaCtRecord.ctt6jg = "";
        }
        if($('input[name="ctt7jg"]:checked').val()){
            OperaCtRecord.ctt7jg = $('input[name="ctt7jg"]:checked').val();
        }else{
            OperaCtRecord.ctt7jg = "";
        }
        if($('input[name="ctt8jg"]:checked').val()){
            OperaCtRecord.ctt8jg = $('input[name="ctt8jg"]:checked').val();
        }else{
            OperaCtRecord.ctt8jg = "";
        }
        if($('input[name="ctt9jg"]:checked').val()){
            OperaCtRecord.ctt9jg = $('input[name="ctt9jg"]:checked').val();
        }else{
            OperaCtRecord.ctt9jg = "";
        }
        if($('input[name="ctt10jg"]:checked').val()){
            OperaCtRecord.ctt10jg = $('input[name="ctt10jg"]:checked').val();
        }else{
            OperaCtRecord.ctt10jg = "";
        }
        if($('input[name="ctt11jg"]:checked').val()){
            OperaCtRecord.ctt11jg = $('input[name="ctt11jg"]:checked').val();
        }else{
            OperaCtRecord.ctt11jg = "";
        }
        OperaCtRecord.ctt1bz = data.field.re1;
        OperaCtRecord.ctt2bz = data.field.re1;
        OperaCtRecord.ctt3bz = data.field.re3;
        OperaCtRecord.ctt4bz = data.field.re4;
        OperaCtRecord.ctt5bz = data.field.re5;
        OperaCtRecord.ctt6bz = data.field.re6;
        OperaCtRecord.ctt7bz = data.field.re7;
        OperaCtRecord.ctt8bz = data.field.re8;
        OperaCtRecord.ctt9bz = data.field.re9;
        OperaCtRecord.ctt10bz = data.field.re10;
        OperaCtRecord.ctt11bz = data.field.re11;

        console.log(JSON.stringify(OperaCtRecord));

        $.ajax({
            type:"POST",
            url:contextPath+"/project/ops/ct/new",
            contentType: "application/json; charset=utf-8",
            data:JSON.stringify(OperaCtRecord),
            dataType:"json",
            success:function (data) {
                console.log(data.message);
                if(data.message == "船体设备效益试验记录表创建成功!"){
                    parent.layer.msg('船体设备效益试验记录表创建成功!<br>页面将在1秒后刷新...');
                    setTimeout(function(){window.location.reload();},1000);
                } else {
                    $('#suresubmitx').attr('disabled',false);
                    parent.layer.msg(data.message, {
                        time:false,
                        btn:"确定",
                        btnAlign: 'c'
                    });
                }
            },
            error:function (e) {
            }
        });
        return false;
    });

    exports('OperaCtRecord', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});